package com.helen.adapter;

import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.helen.entity.RecoveryClassEntity;
import com.helen.shopping.R;
import com.helen.utils.MyLog;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by yang on 2017/11/23.
 */

public class RecoveryClassListAdapter extends BaseQuickAdapter<RecoveryClassEntity, BaseViewHolder> {
    private int selectedPostion = -1;
    private SelectIdListener selectIdListener;
    private ArrayList<Integer> selectId = new ArrayList<>();
    private List<RecoveryClassEntity> selectedDataList = new ArrayList<>();
    private int id;

    public RecoveryClassListAdapter(int layoutResId, List<RecoveryClassEntity> data, ArrayList<Integer> selectId, List<RecoveryClassEntity> selectedDataList) {
        super(layoutResId, data);
        this.selectId = selectId;
        this.selectedDataList = selectedDataList;
    }

    public RecoveryClassListAdapter(int layoutResId, List<RecoveryClassEntity> data) {
        super(layoutResId, data);
    }

    public RecoveryClassListAdapter(int layoutResId, List<RecoveryClassEntity> data, int id) {
        super(layoutResId, data);
        this.id = id;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final RecoveryClassEntity item) {
        try {
            final CheckBox cbItem = (CheckBox) helper.getView(R.id.cb_item);
            cbItem.setText(item.getName());

//            if (id == item.getId()) {
//                cbItem.setEnabled(true);
//            }else {
//                cbItem.setEnabled(false);
//            }

////            单选
//            if (selectedPostion == helper.getAdapterPosition()) {
//                cbItem.setEnabled(true);
//            } else {
//                cbItem.setEnabled(false);
//            }


//            多选
            cbItem.setTag(item);
            cbItem.setChecked(selectId.contains(item.getId()) ? true : false);

            cbItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (cbItem.getTag().equals(item)) {
                        if (cbItem.isChecked()) {
                            if (!selectId.contains(item.getId())) {
                                selectId.add(item.getId());
                                selectedDataList.add(item);
                                if (selectIdListener != null) {
                                    selectIdListener.onSelectIdChange(selectId, selectedDataList);
                                }
                            }
                        } else {
                            if (selectId.contains(item.getId())) {
                                for (int i = 0; i < selectId.size(); i++) {
                                    if (selectId.get(i) == item.getId()) {
                                        selectId.remove(i);
                                        selectedDataList.remove(i);
                                    }
                                }

                                if (selectIdListener != null) {
                                    selectIdListener.onSelectIdChange(selectId, selectedDataList);
                                }
                            }
                        }
                    }
                }
            });

        } catch (Exception e) {
            MyLog.e("yang", "回收分类异常" + e.toString());
        }
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getSelectedPostion() {
        return selectedPostion;
    }

    public void setSelectedPostion(int selectedPostion) {
        this.selectedPostion = selectedPostion;
    }

    public interface SelectIdListener {
        void onSelectIdChange(ArrayList<Integer> selectData, List<RecoveryClassEntity> selectedDataList);
    }

    public void setSelectIdListener(SelectIdListener selectIdListener) {
        this.selectIdListener = selectIdListener;
    }
}
